class_name ControlViewAction
extends Control

#region 属性
@export var label: Label
@export var button: Button
var action_idx:int
var is_waiting_for_input:bool = false
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init_action(idx:int):
	action_idx = idx + InputMap.get_actions().filter(func (x:String):return x.begins_with("ui_")).size()
	button.pressed.connect(start_waiting_for_input)
func update_action():
	var event = InputMap.action_get_events(InputMap.get_actions()[action_idx])[0]
	label.text = InputMap.get_actions()[action_idx]
	if event is InputEventKey:
		button.text = OS.get_keycode_string(event.key_label)
	else:
		button.text = event.as_text()
func start_waiting_for_input():
	GameManager.translate_game_state(GameEnum.GameState.DisableInput)
	button.text = "_"
	is_waiting_for_input = true
	button.disabled = true
func stop_waiting_for_input():
	is_waiting_for_input = false
	button.disabled = false
	GameManager.translate_pre_game_state()
#endregion
#region 私有方法
#endregion
#region 生命周期函数
func _input(event: InputEvent) -> void:
	if is_waiting_for_input and (event is InputEventKey or event is InputEventMouseButton):
		InputMap.action_erase_event(InputMap.get_actions()[action_idx],InputMap.action_get_events(InputMap.get_actions()[action_idx])[0])
		InputMap.action_add_event(InputMap.get_actions()[action_idx],event)
		update_action()
		call_deferred("stop_waiting_for_input")
#endregion
